#!/bin/bash
role=$1
mode=$(cat mode)
echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping $role in $mode mode"
function stop_journalnode(){
  hdfs --daemon stop journalnode
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping journalnode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping journalnode fail"
  fi
}
function stop_namenode(){
  hdfs --daemon stop namenode
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping namenode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping namenode fail"
  fi
}

function stop_zkfc(){
  ZKFC_PID=$(pgrep -f zkfc)
  kill $ZKFC_PID
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping zkfc success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping zkfc fail"
  fi
}

function stop_secondarynamenode(){
  hdfs --daemon stop secondarynamenode
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping secondarynamenode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping secondarynamenode fail"
  fi
}

function stop_datanode(){
  hdfs --daemon stop datanode
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping datanode success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping datanode fail"
  fi
}

function stop_resourcemanager(){
  yarn --daemon stop resourcemanager
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping resourcemanager success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping resourcemanager fail"
  fi
}

function stop_nodemanager(){
  yarn --daemon stop nodemanager
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO stopping nodemanager success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR stopping nodemanager fail"
  fi
}

if [ $role == "all" ];then
  stop_journalnode
  stop_namenode
  stop_secondarynamenode
  stop_datanode
  stop_resourcemanager
  stop_nodemanager
  stop_zkfc
fi

if [ $role == "journalnode" ];then
  stop_journalnode
fi

if [ $role == "namenode" ];then
  stop_namenode
fi

if [ $role == "secondarynamenode" ];then
  stop_secondarynamenode
fi

if [ $role == "datanode" ];then
  stop_datanode
fi

if [ $role == "resourcemanager" ];then
  stop_resourcemanager
fi

if [ $role == "nodemanager" ];then
  stop_nodemanager
fi

if [ $role == "zkfc" ];then
  stop_zkfc
fi